package com.example.controller;

import com.example.entity.Comment;
import com.example.entity.User;
import com.example.entity.res.CommentRes;
import com.example.interceptor.TokenNotValidation;
import com.example.mapper.CommentMapper;
import com.example.mapper.UserMapper;
import com.example.response.R;
import com.example.service.BestService;
import com.example.service.CommentService;
import com.example.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/likeAndComment")
public class LikeCommentController {

    @Autowired
    BestService bestService;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    CommentService commentService;

    @Autowired
    UserMapper userMapper;


    /**
     * 判断本人是否点赞
     *
     * @param request
     * @param rid
     * @return
     */
    @TokenNotValidation
    @GetMapping("/checkWeatherIsSelected/{rid}")
    public Boolean checkWeatherIsSelected(HttpServletRequest request, @PathVariable Integer rid) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        return bestService.checkWeatherIsSelected(userid, rid);
    }

    /**
     * 点赞或取消
     *
     * @param request
     * @param rid
     */
    @TokenNotValidation
    @PutMapping("likeOrCancel/{rid}")
    public void likeOrCancel(HttpServletRequest request, @PathVariable Integer rid) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        Boolean aBoolean = bestService.checkWeatherIsSelected(userid, rid);
        if (aBoolean) {
            bestService.abolishGiveThumbsUp(rid, userid);
        } else {
            bestService.giveThumbsUp(rid, userid);
        }
    }


    /**
     * 获取文章的点赞
     *
     * @param rid
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getBestCountWithRId/{rid}")
    public int getBestCountWithAlbumId(@PathVariable Integer rid) {
        return bestService.getBestCountWithRId(rid);
    }


    /**
     * 通过文章id获得文章的评论
     *
     * @param rid
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getCommentsByRecommend/{rid}")
    public List<CommentRes> getCommentsByRecommend(@PathVariable Integer rid) {
        return commentService.getCommentsByRecommend(rid);
    }

    /**
     * 通过评论的父id获取该父评论的评论者信息
     *
     * @param parentid
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getUserInfoByParentid/{parentid}")
    public User getUserInfoByParentid(@PathVariable Integer parentid) {
        return commentService.getUserInfoByParentid(parentid);
    }

    /**
     * 新增父评论
     *
     * @param comment
     */
    @TokenNotValidation
    @PostMapping("/addFatherComment")
    public void addFatherComment(HttpServletRequest request, @RequestBody Comment comment) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        comment.setUserid(userid);
        comment.setParentid(0);
        comment.setCreattime(new Date());
        commentService.addFatherComment(comment);
    }

    /**
     * 新增子评论
     *
     * @param request
     * @param comment
     */
    @TokenNotValidation
    @PostMapping("/addChildComment")
    public void addChildComment(HttpServletRequest request, @RequestBody Comment comment) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        comment.setUserid(userid);
        comment.setCreattime(new Date());
        commentService.addChildComment(comment);
    }

    /**
     * 删除评论
     *
     * @param request
     * @param commentid
     * @return
     */
    @TokenNotValidation
    @DeleteMapping("/delCommentByCommentId/{commentid}")
    public R delCommentByCommentId(HttpServletRequest request, @PathVariable Integer commentid) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        commentMapper.deleteByParentId(commentid, userid);
        commentMapper.deleteByPrimaryKey(commentid, userid);
        return R.success();
    }

    /**
     * 通过userid获取不含密码的身份信息
     *
     * @param userid
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getUserInfoNotImportant/{userid}")
    public User getUserInfoNotImportant(@PathVariable Integer userid) {
        User user = userMapper.selectByPrimaryKey(userid);
        user.setPassword(null);
        user.setPhone(null);
        user.setRoleId(null);
        return user;
    }
}
